package Gui;

import Oracle.OracleMethods;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alonso
 */
public class CambioDireccionGUI extends javax.swing.JDialog {
    
    private String zona = "";
    private String resultadoDirec = "";
    private String tipo = "";
    
    /** Creates new form CambioDireccionGUI */
    public CambioDireccionGUI(java.awt.Frame parent, boolean modal) throws SQLException {
        super(parent, modal);
        initComponents();
        llenarComboEstado();
        this.setLocationRelativeTo(null);
    }
    
    public CambioDireccionGUI(java.awt.Frame parent, boolean modal, String tipo) throws SQLException {
        super(parent, modal);
        initComponents();
        llenarComboEstado(); 
        this.setLocationRelativeTo(null);
        if(tipo.equals("Estado")){
            comboZona.setEnabled(false);
            comboCiudad.setEnabled(false);
            comboEstado.setEnabled(false);
        }
        if(tipo.equals("Ciudad")){
            comboZona.setEnabled(false);
            comboCiudad.setEnabled(false);
        }
        if(tipo.equals("Zona"))
            comboZona.setEnabled(false);
        
        this.tipo = tipo;
    }
    
    /**
     * @return the zona
     */
    public String getZona() {
        return zona;
    }
    
    public String getResultadoDirec() {
        return resultadoDirec;
    }

    private void llenarComboEstado() throws SQLException{
        comboEstado.removeAllItems();
        comboEstado.addItem("--");
        Connection con = OracleMethods.oracleConnection();
        Statement sentencia = con.createStatement(); 
        ResultSet estadoSet = sentencia.executeQuery("SELECT * FROM ao_direccion WHERE tipo = 'Estado'");
        
        while(estadoSet.next()){
            comboEstado.addItem(estadoSet.getString("nombre"));
        }
        
        sentencia.close();
        estadoSet.close();
        con.close();
    }
    
    private void llenarComboCiudad() throws SQLException{     
        comboCiudad.removeAllItems();
        comboCiudad.addItem("--");
        comboZona.removeAllItems();
        comboZona.addItem("--");
        Connection con = OracleMethods.oracleConnection();
        Statement sentencia = con.createStatement(); 
        ResultSet resultado = sentencia.executeQuery("SELECT * FROM ao_direccion WHERE tipo = 'Estado' AND"
                                                     + " nombre = '" + comboEstado.getSelectedItem() + "'");
        
        if(resultado.next())
            resultado = sentencia.executeQuery("SELECT * FROM ao_direccion WHERE tipo = 'Ciudad' AND "
                                                + "direccion_fk = " + resultado.getString("direccion_pk"));
        
        while(resultado.next()){
            comboCiudad.addItem(resultado.getString("nombre"));
        }
        sentencia.close();
        resultado.close();
        con.close();
    }
    
    private void llenarComboZona() throws SQLException{
        comboZona.removeAllItems();
        comboZona.addItem("--");
        Connection con = OracleMethods.oracleConnection();
        Statement sentencia = con.createStatement(); 
        ResultSet resultado = sentencia.executeQuery("SELECT * FROM ao_direccion WHERE tipo = 'Ciudad' AND"
                                                     + " nombre = '" + comboCiudad.getSelectedItem() + "'");
        
        if(resultado.next())
            resultado = sentencia.executeQuery("SELECT * FROM ao_direccion WHERE tipo = 'Zona' AND "
                                                + "direccion_fk = " + resultado.getString("direccion_pk"));
        
        while(resultado.next()){
            comboZona.addItem(resultado.getString("nombre"));
        }
        sentencia.close();
        resultado.close();
        con.close();
    }
    
    
    
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        comboEstado = new javax.swing.JComboBox();
        comboCiudad = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        comboZona = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Dirección");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Kiosco");

        jTextField1.setEditable(false);
        jTextField1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jTextField1.setText("Kiosco del Este");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Estado");

        comboEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--" }));
        comboEstado.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                comboEstadoMouseReleased(evt);
            }
        });
        comboEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboEstadoActionPerformed(evt);
            }
        });

        comboCiudad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--" }));
        comboCiudad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCiudadActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Ciudad");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Zona");

        comboZona.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--" }));
        comboZona.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboZonaActionPerformed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jButton1.setText("Confirmar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE))
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(comboEstado, 0, 217, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(comboZona, 0, 217, Short.MAX_VALUE)
                                    .addComponent(comboCiudad, 0, 217, Short.MAX_VALUE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(comboEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(comboCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(comboZona, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void comboEstadoMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_comboEstadoMouseReleased
        
}//GEN-LAST:event_comboEstadoMouseReleased

private void comboEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboEstadoActionPerformed
        try {
            llenarComboCiudad();
        } catch (SQLException ex) {
            Logger.getLogger(CambioDireccionGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_comboEstadoActionPerformed

private void comboZonaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboZonaActionPerformed

}//GEN-LAST:event_comboZonaActionPerformed

private void comboCiudadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCiudadActionPerformed
        try {
            llenarComboZona();
        } catch (SQLException ex) {
            Logger.getLogger(CambioDireccionGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_comboCiudadActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
try {
    Connection con = OracleMethods.oracleConnection();
        Statement sentencia = con.createStatement(); 
    ResultSet resultado = null; 
            
    if(tipo.equals("Estado")){
        resultadoDirec = "";
        this.dispose();
        return;
    }
    
    if(tipo.equals("Ciudad"))
    {
        resultado = sentencia.executeQuery("SELECT * FROM ao_direccion WHERE tipo = 'Estado' AND"
                                                 + " nombre = '" + comboEstado.getSelectedItem() + "'");
        if(resultado.next()){
            resultadoDirec = resultado.getString("direccion_pk");
            
        resultado.close();
        sentencia.close();
        con.close();
        
        this.dispose();
        return;
        }
    }
        
    if(tipo.equals("Zona"))
    {
        resultado = sentencia.executeQuery("SELECT * FROM ao_direccion WHERE tipo = 'Ciudad' AND"
                                                 + " nombre = '" + comboCiudad.getSelectedItem() + "'");
        if(resultado.next()){
            resultadoDirec = resultado.getString("direccion_pk");
        resultado.close();
        sentencia.close();
        this.dispose();
        return;
        }
    }
    
        
    
    resultado = sentencia.executeQuery("SELECT * FROM ao_direccion WHERE tipo = 'Zona' AND"
                                                 + " nombre = '" + comboZona.getSelectedItem() + "'");
    if(resultado.next()){
        zona = resultado.getString("direccion_pk");
        resultado.close();
        sentencia.close();
        this.dispose();
    }



} catch (SQLException ex) {
    Logger.getLogger(CambioDireccionGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CambioDireccionGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CambioDireccionGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CambioDireccionGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CambioDireccionGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    CambioDireccionGUI dialog = new CambioDireccionGUI(new javax.swing.JFrame(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                        @Override
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(CambioDireccionGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboCiudad;
    private javax.swing.JComboBox comboEstado;
    private javax.swing.JComboBox comboZona;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

    
}
